package com.xinxing.learning.batch.mybatisplus.step;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinxing.learning.batch.entity.User;
import com.xinxing.learning.batch.mapper.origin.OriginUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.database.AbstractPagingItemReader;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

@Slf4j
public class UserItemReader extends AbstractPagingItemReader<User> {

    @Autowired
    private OriginUserMapper originUserMapper;

    public UserItemReader() {
        // 不设置默认是10
        this.setPageSize(10);
    }

    @Override
    protected void doReadPage() {
        int page = this.getPage() + 1;
        int pageSize = this.getPageSize();
        Page<User> userPage = originUserMapper.selectPage(new Page<>(page, pageSize), null);
        List<User> userList = userPage.getRecords();

        if (this.results == null) {
            this.results = new CopyOnWriteArrayList<>();
        } else {
            this.results.clear();
        }

        this.results.addAll(userList);
    }

    @Override
    protected void doJumpToPage(int i) {

    }
}
